﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Entity.Model;
using OneMile.Entity.Dictionary;
using OneMile.Common;


namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MS0001", Name = "商家登录")]
    public class SLogin :BaseAPI, IProcessMessage
	{
        private DALSLogin dalShopsLogin;
        protected DALSLogin DalShopsLogin
        {
            get
            {
                if (dalShopsLogin == null)
                {
                    dalShopsLogin = DALFactory.getInstance().CreateSLoginDAL();
                }
                return dalShopsLogin;
            }
        }





        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                // 获取参数字段（具体请参见文档）
                string loginName = reqMsg.Body.GetFieldValue("LoginName");
                string pwd = reqMsg.Body.GetFieldValue("Pwd");
                if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(pwd))
                {
                    rspMsg.Head.RspMsg = MessageCode.Login_Faild_Empty.Msg;
                    rspMsg.Head.RspCode = MessageCode.Login_Faild_Empty.Code;
                    return rspMsg;
                }
                else if (!DalShopsLogin.IsShopsExist(loginName))
                {
                    rspMsg.Head.RspMsg = MessageCode.Login_Faild_Not_Exist.Msg;
                    rspMsg.Head.RspCode = MessageCode.Login_Faild_Not_Exist.Code;
                    return rspMsg;
                }
                EShopsInfo info = DalShopsLogin.ShopsLogin(loginName, pwd);
                if (info == null)
                {
                    rspMsg.Head.RspMsg = MessageCode.Login_Faild.Msg;
                    rspMsg.Head.RspCode = MessageCode.Login_Faild.Code;
                    return rspMsg;
                }

                rspMsg.Body.AddField(DShopsInfo.ShopsName, info.ShopsName);
                rspMsg.Body.AddField(DShopsInfo.ShopsId, info.ShopsId.ToString());
                rspMsg.Body.AddField(DShopsInfo.ShopsTel, info.ShopsTel);
                rspMsg.Body.AddField(DShopsInfo.LoginName, info.LoginName);
                rspMsg.Body.AddField(DShopsInfo.Password, info.Password);

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("ShopLogin" + e.Message);
                throw e;
            }
        }
        #endregion
    }
}